//
//  AIMenuViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/4.
//

import UIKit

class AIMenuViewController: BaseViewController {
    var chooseType:( (Int) -> Void)?
    var position:CGPoint?
    
    private lazy var topStackView:UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal // 设置为水平排列
        stackView.distribution = .fillEqually
        stackView.spacing = 0 // 设置视图之间的间距
        stackView.backgroundColor = UIColor(hexString: "#111112")
        stackView.allRound(8)
        return stackView
    }()
    
    var copyView = MenuItemView()
    var deleteView = MenuItemView()
    var collectView = MenuItemView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.clear
        // 创建并添加手势识别器
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(close(_:)))
        self.view.addGestureRecognizer(tapGestureRecognizer)
        self.view.addSubview(topStackView)
        var y = self.position?.y ?? 0.0
        var x = self.position?.x ?? 0.0
        
        
        // 获取导航栏高度
        let navHig = navHig()
        
        // 确保 y 不低于导航栏高度 + 20
        y = max(y, navHig + 20)
        
        copyView.setup(type: 1)
        deleteView.setup(type: 2)
        collectView.setup(type: 3)
        topStackView.addArrangedSubviews([copyView, deleteView, collectView])
        x = x + 40
        // 设定最大可接受的 x 值，防止超出屏幕
        let maxX = max(0, SCREEN_WIDTH - 184)  // 164 是 topStackView 的宽度
        self.topStackView.snp.remakeConstraints { make in
            make.top.equalToSuperview().offset(y + 54)
            make.leading.equalToSuperview().offset(min(x, maxX))  // 确保 x 不超过 maxX
            make.height.equalTo(64)
            make.width.equalTo(164)
        }
        
        
        copyView.setOneTappedWithCallback_k { [weak self] in
            self?.chooseType?(1)
            self?.dismiss(animated: true)
        }
        
        deleteView.setOneTappedWithCallback_k { [weak self] in
            self?.chooseType?(2)
            self?.dismiss(animated: true)
        }
        
        collectView.setOneTappedWithCallback_k { [weak self] in
            self?.chooseType?(3)
            self?.dismiss(animated: true)
        }
    }
    
    
    @objc func close(_ gesture: UITapGestureRecognizer) {
        let point = gesture.location(in: self.view)
        if topStackView.frame.contains(point) {
            return
        }
        self.dismiss(animated: false, completion: nil)
    }
}

class MenuItemView: UIView {
    
    private lazy var iconIV: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "")
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private lazy var lockLb: UILabel = {
        let label = UILabel()
        label.textColor = .white
        label.font = UIFont.pingFangSCRegular(ofSize: 11)
        label.textAlignment = .center
        return label
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubviews([iconIV, lockLb])
        iconIV.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(12)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(20)
        }
        lockLb.snp.makeConstraints { make in
            make.top.equalTo(iconIV.snp.bottom).offset(8)
            make.leading.trailing.equalToSuperview()
        }
    }
    
    func setup(type:Int){
        if type == 1 {
            iconIV.image = UIImage(named: "icon_home_copy")
            lockLb.text = "复制"
        }else if type == 2 {
            iconIV.image = UIImage(named: "icon_home_delete")
            lockLb.text = "删除"
        }else {
            iconIV.image = UIImage(named: "icon_home_collect")
            lockLb.text = "收藏"
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
}
//import UIKit
//
//class CustomTransitioningDelegate: NSObject, UIViewControllerTransitioningDelegate {
//    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
//        return CustomPresentationController(presentedViewController: presented, presenting: presenting)
//    }
//
//    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//        return CustomPresentationAnimationController(isPresenting: true)
//    }
//
//    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
//        return CustomPresentationAnimationController(isPresenting: false)
//    }
//}
//
//class CustomPresentationAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
//    private let isPresenting: Bool
//
//    init(isPresenting: Bool) {
//        self.isPresenting = isPresenting
//    }
//
//    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
//        return 0.3
//    }
//
//    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
//        guard let fromView = transitionContext.view(forKey: .from),
//              let toView = transitionContext.view(forKey: .to) else {
//            return
//        }
//
//        let containerView = transitionContext.containerView
//        let duration = transitionDuration(using: transitionContext)
//
//        if isPresenting {
//            containerView.addSubview(toView)
//            toView.frame = containerView.bounds.offsetBy(dx: 0, dy: containerView.bounds.height)
//            UIView.animate(withDuration: duration, animations: {
//                toView.frame = containerView.bounds
//            }) { finished in
//                transitionContext.completeTransition(finished)
//            }
//        } else {
//            UIView.animate(withDuration: duration, animations: {
//                fromView.frame = containerView.bounds.offsetBy(dx: 0, dy: containerView.bounds.height)
//            }) { finished in
//                fromView.removeFromSuperview()
//                transitionContext.completeTransition(finished)
//            }
//        }
//    }
//}
//
//class CustomPresentationController: UIPresentationController {
//    override var shouldRemovePresentersView: Bool {
//        return false
//    }
//
//    override var frameOfPresentedViewInContainerView: CGRect {
//        guard let containerView = containerView else { return .zero }
//        return containerView.bounds
//    }
//}
